(function ($) { // introduce jQuery first.
    //requires IE 9 or above, or another web browser that is modern enough.
    var re = /(\/\*|"|'|\*\/|\/\/|(?:$|\n)|\\)/,
        keywords = /\b(var|if|else|return|null|function|for|while|do|true|false|this|switch|case|break|default|try|catch|throw|finally|in|with|new|delete)\b/g,
        created=$("<div>");
    function WrapKey(/* String */ x) {
        return x.replace(keywords,"<span class=k>$1</span>");
    }
    function Tokenize(/* String */ x) {
        var arr = x.split(re), stack = [], i, len = arr.length, esc = false;
        stack.top = function () {
            return this[this.length - 1] || null;
        }
        for (i = 0; i < len; i++) {
            switch (arr[i]) {
                case "/*":
                    if (!stack.top()) { // multiline comment begins.
                        stack.push("mc");
                        arr[i] = "<span class=c>/*";
                    }
                    break;
                case "\"":
                    if (esc) {
                        esc = false;
                        break;
                    }
                    if (!stack.top()) { // string begins with ".
                        stack.push("ds");
                        arr[i]="<span class=s>\""
                    } else if (stack.top() == "ds") { // string beginning with " ends.
                        stack.pop();
                        arr[i] = "\"</span>";
                    }
                    break;
                case "'":
                    if (esc) {
                        esc = false;
                        break;
                    }
                    if (!stack.top()) { // string begins with ".
                        stack.push("ss");
                        arr[i] = "<span class=s>'"
                    } else if (stack.top() == "ss") { // string beginning with " ends.
                        stack.pop();
                        arr[i] = "'</span>";
                    }
                    break;
                case "*/":
                    if (stack.top() == "mc") { // multiline comment ends.
                        stack.pop();
                        arr[i] = "*/</span>";
                    }
                    break;
                case "\\":
                    if (stack.top() == "ds" || stack.top() == "ss") { // escape starts or ends.
                        esc = !esc;
                    }
                    break;
                case "//":
                    if (!stack.top()) { // single-line comment starts.
                        stack.push("sc");
                        arr[i]="<span class=c>//"
                    }
                    break;
                case "\n":
                    if (esc) break;
                    if (stack.top() == "sc" || stack.top() == "ds" || stack.top() == "ss") {
                        stack.pop();
                        arr[i] = "</span>\n";
                    }
                    break;
                case "":
                    if (i == arr.length - 1) {
                        stack.top() && stack.pop();
                        arr[i] += "</span>";
                    }
                    break;
                default:
                    if (!stack.top()) {
                        arr[i] = WrapKey(arr[i]);
                    } else {
                        esc = false;
                    }
                    if (i == arr.length - 1) {
                        stack.top() && stack.pop();
                        arr[i] += "</span>";
                    }
            }
        }
        return arr.join("")+"\n";
    }
    $.fn.syntaxHighlight = function (x) {
        var str = created.text($(this).val()).html();
        $(x).html(Tokenize(str));
        return this;
    };
    $.fn.syntaxHighlightFrom = function (/* String */ x) {
        var str = created.text(x).html();
        $(this).html(Tokenize(str));
        return this;
    }
})(window.jQuery || {});